from django.db import models
from backend.UserAdmin.models import User


class CellType(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name="细胞类型")

    def __str__(self):
        return self.name


class Freezer(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name="冰柜名")
    number = models.IntegerField(verbose_name="编号")
    location = models.CharField(max_length=20, verbose_name="位置")

    def __str__(self):
        return f"{self.name} (No.{self.number})"


class Level(models.Model):
    freezer = models.ForeignKey(
        Freezer,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="所属冰柜"
    )
    number = models.IntegerField(verbose_name="层数")

    class Meta:
        unique_together = ('freezer', 'number')

    def __str__(self):
        return f"{self.freezer} - 第{self.number}层"


class Column(models.Model):
    level = models.ForeignKey(
        Level,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="所属层"
    )
    number = models.IntegerField(verbose_name="列数")

    class Meta:
        unique_together = ('level', 'number')

    def __str__(self):
        return f"{self.level} - 第{self.number}列"


class Drawer(models.Model):
    column = models.ForeignKey(
        Column,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="所属列"
    )
    number = models.IntegerField(verbose_name="编号")

    class Meta:
        unique_together = ('column', 'number')

    def __str__(self):
        return f"{self.column} - 抽屉{self.number}"


class Box(models.Model):
    drawer = models.ForeignKey(
        Drawer,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="所属抽屉"
    )
    number = models.IntegerField(verbose_name="编号")

    class Meta:
        unique_together = ('drawer', 'number')

    def __str__(self):
        return f"{self.drawer} - 箱{self.number}"


class Cell(models.Model):
    box = models.ForeignKey(
        Box,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="所属箱"
    )
    row_number = models.IntegerField(verbose_name="行数")
    column_number = models.IntegerField(verbose_name="列数")
    number = models.IntegerField(verbose_name="编号")
    is_occupied = models.BooleanField(default=0, verbose_name="是否被占用")

    class Meta:
        unique_together = ('box', 'row_number', 'column_number')

    def __str__(self):
        return f"{self.box} - 位置({self.row_number},{self.column_number})"


class Sample(models.Model):
    cell_type = models.ForeignKey(
        CellType,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="细胞类型"
    )
    entry_time = models.DateTimeField(auto_now_add=True, verbose_name="入库时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    name = models.CharField(max_length=20, verbose_name="名称",default="未命名")
    entry_user = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="采样人"
    )
    cell = models.ForeignKey(
        Cell,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="存储位置"
    )

    def __str__(self):
        return f"{self.cell_type}样本(位置:{self.cell})"
